﻿using System;
using System.IO;

/// <summary>
/// 邮件附件
/// </summary>
public class EmailAttachment
{
    /// <summary>
    /// 附件文件名
    /// </summary>
    public string FileName { get; set; } = string.Empty;

    /// <summary>
    /// 附件内容（字节数组）
    /// </summary>
    public byte[] Content { get; set; } = Array.Empty<byte>();

    /// <summary>
    /// 内容类型（如：application/pdf, image/jpeg等）
    /// </summary>
    public string ContentType { get; set; } = "application/octet-stream";

    /// <summary>
    /// 附件大小（字节）
    /// </summary>
    public long Size => Content?.Length ?? 0;

    /// <summary>
    /// 内容ID（用于HTML邮件中嵌入图片）
    /// </summary>
    public string ContentId { get; set; } = string.Empty;

    /// <summary>
    /// 是否内联附件（嵌入式图片等）
    /// </summary>
    public bool IsInline { get; set; }

    public EmailAttachment() { }

    public EmailAttachment(string fileName, byte[] content, string contentType = null)
    {
        FileName = fileName;
        Content = content;
        ContentType = contentType ?? GetContentTypeFromFileName(fileName);
    }

    private string GetContentTypeFromFileName(string fileName)
    {
        var extension = Path.GetExtension(fileName)?.ToLower();
        return extension switch
        {
            ".pdf" => "application/pdf",
            ".jpg" or ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".txt" => "text/plain",
            ".doc" => "application/msword",
            ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            ".xls" => "application/vnd.ms-excel",
            ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            ".zip" => "application/zip",
            ".rar" => "application/x-rar-compressed",
            _ => "application/octet-stream"
        };
    }
}